package com.heiyanquan.security.browser.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author dingaolin
 * @Date: 2017/12/9 0009.
 */
@Service
public class MyUserDetailsService implements UserDetailsService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        logger.info("登录用户名：" + userName);
        // TODO: 2017/12/9 0009  根据用户名查询用户信息,这部分需要从数据库中读取
        // 根据查找到的用户信息判断用户是否被冻结等等信息
        // 这里用的 spring security 自带的 User 对象，可以自定义 User 对象，只要实现 UserDetails 接口即可
        // passwordEncoder.encode("1234560") 这是密码加密，需要从数据库中取，数据库中存放密码加密后的字符串

        logger.info("数据库密码：" + passwordEncoder.encode("123456"));
        return new User(userName,passwordEncoder.encode("123456"),
                true,true,true,true,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));
    }
}
